import api from './index'

export const taskApi = {
  // 创建任务
  async createTask(taskData) {
    const response = await api.post('/tasks/create', taskData)
    return response
  },

  // 获取任务列表
  async getTasks(params = {}) {
    const queryString = new URLSearchParams(params).toString()
    const response = await api.get(`/tasks/list?${queryString}`)
    return response
  },

  // 获取单个任务详情
  async getTask(id) {
    const response = await api.get(`/tasks/${id}`)
    return response
  },

  // 启动任务
  async startTask(id) {
    const response = await api.post(`/tasks/${id}/start`)
    return response
  },

  // 停止任务
  async stopTask(id) {
    const response = await api.post(`/tasks/${id}/stop`)
    return response
  },

  // 重试任务
  async retryTask(id, force = false) {
    const response = await api.post(`/tasks/${id}/retry`, { force })
    return response
  },

  // 删除任务
  async deleteTask(id) {
    const response = await api.delete(`/tasks/${id}`)
    return response
  },

  // 批量启动任务
  async batchStart(taskIds) {
    const response = await api.post('/enhanced-tasks/batch/start', {
      task_ids: taskIds
    })
    return response
  },

  // 批量停止任务
  async batchStop(taskIds) {
    const response = await api.post('/enhanced-tasks/batch/stop', {
      task_ids: taskIds
    })
    return response
  },

  // 获取任务统计
  async fetchStats() {
    const [overviewResponse, retryResponse] = await Promise.all([
      api.get('/enhanced-tasks/stats/overview'),
      api.get('/enhanced-tasks/stats/retry')
    ])
    
    return {
      stats: overviewResponse,
      retryStats: retryResponse
    }
  }
}

export default taskApi